var express = require("express");
var jwt = require("jsonwebtoken");
const myroute = express.Router();
const mySql = require("../model/index");


myroute.get('/get', function (request, response) {
    // response.send('账号密码错误')
    console.log(request.query);
    const sql = `SELECT * FROM  postadmin
    LEFT JOIN admin ON admin.jobs=postadmin.post_id
    LEFT JOIN department ON postadmin.post_department = department.id 
    WHERE account='${request.query.account}'&& password ='${request.query.password}'`
    mySql.query(sql, function (err, data) {
        // console.log(data[0]);
        // console.log(err, data);
        if (err) {
            console.log(err);
        } else {
            if (data.length) {
                // 有用户
                response.json({ 
                    code: 200, 
                    data: data, 
                    token: jwt.sign({ data: data[0] }, 'web281') 
                })
                // console.log(123);
            } else {
                // 没有用户
                response.send('账号密码错误')
            }
        }
    })

})

// 忘记密码
myroute.post('/edit', function (request, response) {
    // response.send('账号密码错误')
    const sql = `select * from admin where phone='${request.body.phone}' `
    const sql2 = `update admin set password='${request.body.jiu}' where phone='${request.body.phone}'`
    mySql.query(sql, function (err, data) {
        // console.log(err, data);
        if (err) {
            console.log(err);
        } else {
            if (data.length) {
                // 有用户
                // response.send(data)
                mySql.query(sql2, function (err2, data2) {
                    if (err2) {
                        console.log(err2);
                    } else {
                        // console.log(data);
                        if (data2) {
                            response.send({ code: 200, msg: '修改成功' })
                        } else {
                            response.send('修改密码失败！')
                        }
                    }
                })
            } else {
                // 没有用户
              response.send("电话号码不存在！");
            }
        }
    })

})

// 注册用户的接口
myroute.get('/login2', function (req, res) {
    //增加用户的sql
    console.log("心点击了一次");
    let pp = [];    //用户id的数组
    let p = 0;      //新用户最终的账号

    const sql = `SELECT work_id FROM admin;`;
    
    mySql.query(sql, function (err, data) {
        // console.log(err, data);
        if (err) {
            console.log(err);
        } else {
            if (data.length) {
                // 如果用户库中有用户，那么就将用户ID最大的获取出来，并且经过处理变成新添加的用户账号

                // 将取出来的idpush进pp数组中
                data.forEach((key) => {
                    pp.push(key.work_id);
                });
                pp.sort((x,y)=>x-y)     //排序，从小到大
                console.log(pp);
                console.log(pp[pp.length]);
                console.log(pp[pp.length-1]);
                p = pp[pp.length-1]
                p -= 99999;

            } else {
                // 如果没有用户，那就设置新账户为100001；
                // response.send('电话号码不存在！')
                p = 100001;
            }
            const sql2 = `INSERT INTO admin VALUES(NULL,${p},${req.query.password},"试用","未填写","未填写",12,"男",${Number(req.query.phone)},"未填写",1,"2022-9-1 14:00:00","未填写","普通员工","2022-9-1","5000");`
            mySql.query(sql2, function (err2, data2) {
                console.log("开始注册用户");
                if (err2) {
                    console.log(sql2);
                    console.log(err2);
                    res.send('注册失败！')

                } else {
                    console.log("用作试验");
                    if (data2) {
                        console.log(data2.insertId);
                        res.send({ code: 200, msg: '注册成功' , account:p})
                        // console.log(data2);
                    } else {
                        res.send('注册失败！')
                    }
                }
            })

        }
    })

})

// 修改密码
myroute.get('/amend', function (req, res) {
    console.log(req.query);
    const sql = `SELECT * FROM admin WHERE work_id = "${req.query.work_id}" AND PASSWORD="${req.query.password1}";`;
    const sql1 = `UPDATE admin SET PASSWORD="${req.query.password2}" WHERE work_id="${req.query.work_id}";`;
    mySql.query(sql, function (err, data) {
        if (err) {
            console.log(err);
            console.log("错误了");
        } else {
            console.log(data);
            if (data.length) {
                mySql.query(sql1,function(err,data2){
                    if(err){
                        console.log(err);
                        console.log("错误2");
                    }else{
                        // console.log(data2);
                        res.send({ code: 200, msg: '修改密码成功' , data:data})
                    }
                })
            } else {
                console.log("没有这个账号");
                res.send("修改密码失败")
                // response.send('电话号码不存在！')
            }
        }
    })

})


module.exports = myroute;
